//
//  FilterType_Vc.swift
//  Pods
//
//  Created by eme on 2017/3/30.
//
//


/*
 owner:cy
 update:2017年03月30日16:38:25
 info:类目筛选界面
 */
import RxSwift
import UtilCore

class FilterType_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    @IBOutlet weak var filter_Tv: UITableView!
    
    @IBOutlet weak var reset_Btn: UIButton!
    
    @IBOutlet weak var submit_Btn: UIButton!
    /*----------------------------   自定义属性区域    ----------------------------*/
    var ptlist: [PtlistModel]?
    /// 用于保存选中的model
    var hasSelected_ptModel:PtlistModel?
    /// 点击重置和确定按钮时都会触发
    var didSelectPtModel_Closure:((_ PtModel:PtlistModel?) ->Void)?
    
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.submit_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                if let didSelectPtModel_Closure = self.didSelectPtModel_Closure{
                    didSelectPtModel_Closure(self.hasSelected_ptModel)
                }
               _ = self.navigationController?.popViewController(animated: true)
            })
            .disposed(by: disposeBag)
        self.reset_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                if let didSelectPtModel_Closure = self.didSelectPtModel_Closure{
                    didSelectPtModel_Closure(nil)
                }
                self.hasSelected_ptModel = nil
                self.filter_Tv.reloadData()
            })
            .disposed(by: disposeBag)
    }
    /**
     app 主题 设置
     */
    override func setViewTheme(){
        
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
    }
}
// MARK: UITableViewDelegate
extension FilterType_Vc :UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        guard let ptlist = self.ptlist  else {
            return 0
        }
        return ptlist.count + 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "FilterPt_TCell", for: indexPath) as! FilterPt_TCell
        guard let hasSelectedModel = self.hasSelected_ptModel else {
            if indexPath.row == 0 {
                cell.ptname_Lb.text = "不限"
                cell.updateCellState(isSelected: true)
            }else{
                cell.updateCellState(isSelected: false)
                cell.ptname_Lb.text = self.ptlist![indexPath.row - 1].ptname
            }
            return cell
        }
        if indexPath.row == 0 {
            cell.ptname_Lb.text = "不限"
            cell.updateCellState(isSelected: false)
        }else{
            let item = self.ptlist![indexPath.row - 1]
            cell.ptname_Lb.text = item.ptname
            cell.updateCellState(isSelected: item.ptid == hasSelectedModel.ptid)
        }
        return cell
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
}
// MARK: UITableViewDelegate
extension FilterType_Vc: UITableViewDelegate {
    /// 点击行事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        if indexPath.row == 0 {
            self.hasSelected_ptModel = nil
        }else{
            self.hasSelected_ptModel = self.ptlist![indexPath.row - 1]
        }
        self.filter_Tv.reloadData()
    }
    
}
